package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.UacResidentMapper;
import com.csun.cmny.provider.model.domain.UacResident;
import com.csun.cmny.provider.model.dto.resident.ResidentDto;
import com.csun.cmny.provider.model.dto.resident.ResidentQueryDto;
import com.csun.cmny.provider.service.UacResidentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class UacResidentServiceImpl extends BaseService<UacResident> implements UacResidentService {

    @Resource
    private UacResidentMapper uacResidentMapper;

    @Override
    public Integer addResident(ResidentDto residentDto, LoginAuthDto loginAuthDto) {
        residentDto.setGroupId(loginAuthDto.getGroupId());
        UacResident uacResident = new UacResident();
        BeanUtils.copyProperties(residentDto, uacResident);
        return uacResidentMapper.insert(uacResident);
    }

    @Override
    public Integer updateResident(ResidentDto residentDto, LoginAuthDto loginAuthDto) {
        UacResident uacResident = new UacResident();
        BeanUtils.copyProperties(residentDto, uacResident);
        return uacResidentMapper.updateByPrimaryKeySelective(uacResident);
    }

    @Override
    public ResidentDto showResident(Long residentId) {
        uacResidentMapper.selectByPrimaryKey(residentId);
        return null;
    }

    @Override
    public Integer deleteResidentById(Long residentId) {
        return uacResidentMapper.deleteByPrimaryKey(residentId);
    }

    @Override
    public PageInfo listPage(ResidentQueryDto residentQueryDto) {
        PageHelper.startPage(residentQueryDto.getPageNum(), residentQueryDto.getPageSize());
        List<UacResident> uacResidentList = uacResidentMapper.listPage(residentQueryDto);
        return new PageInfo<>(uacResidentList);
    }
}
